﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.DataContract.WorkFlow;
using HF.Framework.DataClientDBAgent;
using System.Data;

namespace HF.WorkFlow.Engine
{
    public class Parameter
    {
         #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "ID","AppModel","Name", "Description", "Value", "Sataus"};
        private string tableName = "HF_Parameter";
        private string keyField = "ID";
        private string sqlString = "";

        #endregion

         #region  私有方法
        private void setParameter(ParameterEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@ID", entity.ID, typeof(string));
            sqlDataItem.AppendParameter("@AppModel", entity.Name, typeof(string));
            sqlDataItem.AppendParameter("@Name", entity.Name, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@Value", entity.Value, typeof(string));
            sqlDataItem.AppendParameter("@Sataus", entity.Sataus, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

     
        #endregion

        #region  构造函数
        public Parameter()
        { }
        public static Parameter GetInstance()
        {
            return new Parameter();
        }
        #endregion

        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(ParameterEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(ParameterEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="accreditId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string accreditId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, accreditId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region  扩展方法
        //此处添加扩展方法
        public int AddParameter(List<ParameterEntity> parameter)
        {
            try
            {
                int i = 0;
                if (parameter.Count > 0)
                {
                    ClientDBAgent agent = new ClientDBAgent();
                    foreach (var item in parameter)
                    {
                        ParameterEntity entity = new ParameterEntity();
                        entity.ID = Guid.NewGuid().ToString();
                        entity.AppModel = item.AppModel;
                        entity.Name = item.Name;
                        entity.Description = item.Description;
                        entity.Value = item.Value;
                        entity.Sataus = item.Sataus;
                        i = GetInstance().Insert(entity);
                    }
                }
                return i;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查询参数方法
        /// </summary>
        /// <param name="appModel">模块</param>
        /// <returns></returns>
        public DataTable GetParameter(string appModel)
        {
            try
            {
                string sql = "select * from HF_Parameter  where AppModel= @AppModel ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@AppModel", appModel);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #endregion
    }
}
